package com.ibm.cloud.redismq.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageListener extends MessageWorkerBase {
	@Autowired MessageConsumer<?> consumer;
	byte[] queueKey;
	byte[] workingQKey;
	@Override
	protected void init() {
		queueKey = properties.getRedisQueueName().getBytes();
		workingQKey = properties.getRedisWorkQueueName().getBytes();
	}
	
	@Override
	protected void loopedWork() {
		// 1.监听队列取数据
		byte[] data = connection.bRPopLPush(0, queueKey, workingQKey);
		if (log.isTraceEnabled()) {
			log.trace("{} rows removed from working queue", new String(data));
		}
		// 2.消费数据
		consumer.onMessage(null);
		// 3.从工作队列中清除数据
		Long cnt = connection.lRem(workingQKey, 1l, data);
		log.debug("{} rows removed from working queue", cnt);
	}

}
